package com.lfy.modules.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lfy.modules.blog.domain.ArticleTagEntity;
import com.lfy.modules.blog.domain.vo.article.TagListVO;
import com.lfy.modules.blog.service.ArticleTagService;
import com.lfy.modules.blog.mapper.BlogArticleTagMapper;
import com.lfy.modules.common.domain.R;
import com.lfy.modules.common.domain.entity.UserEntity;
import com.lfy.modules.common.intercept.AuthHandlerInterceptor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 吕飞扬
* @description 针对表【blog_article_tag】的数据库操作Service实现
* @createDate 2024-05-02 16:28:58
*/
@Service
public class ArticleTagServiceImpl extends ServiceImpl<BlogArticleTagMapper, ArticleTagEntity>
    implements ArticleTagService {

    @Override
    public R<List<TagListVO>> getTags() {
        UserEntity userEntity = AuthHandlerInterceptor.authLocal.get();

        List<ArticleTagEntity> list = this.list(new LambdaQueryWrapper<ArticleTagEntity>().eq(ArticleTagEntity::getTenantId,userEntity.getId()));
        if(CollectionUtils.isEmpty(list)){
            return R.success();
        }
        return R.success(list.stream().map(tag -> {
            TagListVO tagListVO = new TagListVO();
            BeanUtils.copyProperties(tag, tagListVO);
            return tagListVO;
        }).collect(Collectors.toList()));

    }

    @Override
    public R<Void> addTag(String tagName) {
        ArticleTagEntity articleTagEntity = new ArticleTagEntity();
        articleTagEntity.setTagName(tagName);
        UserEntity userEntity = AuthHandlerInterceptor.authLocal.get();

        articleTagEntity.setTenantId(userEntity.getId());
        this.save(articleTagEntity);
        return R.success();
    }

    @Override
    public R<Void> deleteTag(Long tagId) {

        this.removeById(tagId);
        return R.success();
    }


}




